package luj.tool.jnproxyan.proxy.network.socks.actor.root.message.http.proxy2s.receive.read;

import luj.tool.jnproxyan.proxy.network.socks.actor.root.Socks5Actor;
import luj.tool.jnproxyan.proxy.stats.http.history.HistoryCurItemGetter;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryDat;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
final class OnP2sRecvRead implements Socks5Actor.Handler<P2sRecvReadMsg> {

  @Override
  public void onHandle(Context ctx) throws Exception {
    Socks5Actor self = ctx.getActorState(this);
    P2sRecvReadMsg msg = ctx.getMessage(this);

    // 更新历史统计
    HttpHistoryDat historyDat = self.getHttpHistoryDat();
    HttpHistoryItem curItem = _curItemGetter.inject(historyDat, msg.socksId()).getCurrent();
    curItem.setBytesServer2p(curItem.getBytesServer2p() + msg.byteCount());
  }

  @Autowired
  HistoryCurItemGetter _curItemGetter;
}
